package net.sf.l2j.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import net.sf.l2j.database.L2DatabaseFactory;
import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/*
 * Author: M-095, L2EmuRT Dev Team
 */

public class ServiceLog {
	private static Log _log = LogFactory.getLog(ServiceLog.class.getName());
	private static String _charName = null;
	private static int _charId = 0;

	public static void ServiceAudit(L2PcInstance player, String serviceName,
			String message) {
		L2PcInstance activeChar = player;
		_charName = activeChar.getName();
		_charId = activeChar.getObjectId();
		storeInDB(serviceName, _charName, _charId, message);
	}

	public static void storeInDB(String serviceName, String charName,
			int charId, String message) {
		Connection con = null;
		PreparedStatement statement = null;
		try {
			con = L2DatabaseFactory.getInstance().getConnection();
			statement = con
					.prepareStatement("INSERT INTO service_log(serviceName, charName, charId, message, date) VALUES(?,?,?,?,now())");
			statement.setString(1, serviceName);
			statement.setString(2, charName);
			statement.setInt(3, charId);
			statement.setString(4, message);
			statement.executeUpdate();
		} catch (Exception e) {
			_log.fatal("Could not store Service Action:", e);
		} finally {
			try {
				if (con != null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}